const Service = require("egg").Service;

class NoteService extends Service {
  // 获取模板列表
  async getNoteList() {
    const { app } = this;
    try {
      return await app.model.Note.findAll({
        attributes: ["id", "title", "content", "createdAt", "updatedAt"],
        where: { delFlag: 0 },
      });
    } catch (err) {
      console.log(err);
      throw new Error(err);
    }
  }

  async getSelfNoteList(query = {}) {
    const { app, ctx } = this;
    const { id: user_id } = ctx.decode;
    try {
      let params = {
        attributes: ["id", "title", "content", "created_time", "updated_time"],
        where: { delFlag: 0, user_id },
      };
      let needPaging = query.currentPage && query.pageSize;
      if (needPaging) {
        let totalNum = await app.model.Note.count({
          where: { delFlag: 0 },
        });
        params.offset = (query.currentPage - 1) * query.pageSize;
        params.limit = parseInt(query.pageSize);
        let noteList = await app.model.Note.findAll(params);
        return {
          noteList,
          totalNum,
          totalPages: Math.ceil(totalNum / query.pageSize),
        };
      } else {
        return await app.model.Note.findAll(params);
      }
    } catch (err) {
      console.log(err);
      throw new Error(err);
    }
  }

  // 保存笔记
  async saveNote({ id, title, content, user_id }) {
    const { app } = this;
    try {
      if (!id) {
        return await app.model.Note.create({
          title,
          content,
          user_id,
        });
      } else {
        const exit = await app.model.Note.findByPk(id);
        if (!exit) {
          this.ctx.logger.debug(`根据${id}无法查询到模板`);
          throw new Error("无法查询到模板");
        }
        exit.update({ title, content, user_id });
        return true;
      }
    } catch (err) {
      console.log(err);
      throw new Error(err);
    }
  }

  //删除笔记
  async deleteNote(id) {
    const { app } = this;
    try {
      const exit = await app.model.Note.findByPk(id);
      if (!exit) {
        this.ctx.logger.debug(`根据${id}无法查询到模板`);
        throw new Error("无法查询到模板");
      }
      exit.update({ delFlag: 1 });
      return true;
    } catch (err) {
      console.log(err);
      throw new Error(err);
    }
  }

  //查询笔记
  async findNoteById(id) {
    const { app } = this;
    try {
      return await app.model.Note.findOne({
        where: { id },
      });
    } catch (err) {
      console.log(err);
      throw new Error(err);
    }
  }

  //修改笔记
  async modifyNote({ id, title, content, user_id }) {
    const { app } = this;
    try {
      if (!id) {
        return await app.model.Note.create({
          title,
          content,
          user_id,
          created_time: new Date(),
          updated_time: new Date(),
        });
      } else {
        const exit = await app.model.Note.findByPk(id);
        if (!exit) {
          this.ctx.logger.debug(`根据${id}无法查询到模板`);
          throw new Error("无法查询到模板");
        }
        exit.update({ title, content, user_id, updated_time: new Date() });
        return true;
      }
    } catch (err) {
      console.log(err);
      throw new Error(err);
    }
  }
}

module.exports = NoteService;
